﻿Imports CrystalDecisions.CrystalReports.Engine

Public Class FrmViewReport
    Public pCodeEtat As Integer
    Public pParamRPT As String

    Private szSQL As String
    Private rs As ADODB.Recordset

    Private Sub FrmViewReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim szCheminRPT As String = ""
        Dim szNomRPT As String = ""
        Dim szCriteresRPT As String = ""

        ' lire les infos de l'état
        szSQL = "CALL spGet_P_TABLES_byCODE('P_ETATS'," & pCodeEtat & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount < 1 Then
            MsgBox("Etat " & pCodeEtat & " : non trouvé dans la table P_ETATS!", MsgBoxStyle.Critical)
            Exit Sub
        End If

        ' afficher le nom du RPT sur la barre en-tête
        Me.Text = rs.Fields("libelle").Value & "  (" & szCheminRPT & ")"
        szNomRPT = rs.Fields("nom_rpt").Value
        szCheminRPT = Mid(My.Application.Info.DirectoryPath, 1, 10) & "\ETATS\" & rs.Fields("nom_rpt").Value
        szCriteresRPT = FormaterSelection(nv(rs.Fields("criteres").Value))
        rs.Close()

        ' edition d'un courrier ?
        If szNomRPT = "courrier" Then
            szCheminRPT = pParamRPT
            pParamRPT = ""      ' RAB le paramètre
        End If


        Dim rpt As New ReportDocument
        Try
            rpt.Load(szCheminRPT)
        Catch ex As Exception
            MsgBox(ex.Message & " : " & vbCrLf & szCheminRPT, vbCritical)
            Exit Sub
        End Try
        ' définit la sélection
        If szCriteresRPT.Length > 0 Then
            rpt.RecordSelectionFormula = szCriteresRPT
        End If
        ' definit le(s) paramètre(s) éventuel(s)
        If pParamRPT.Length > 0 Then
            rpt.SetParameterValue(0, pParamRPT)
        End If
        ' Editer
        CrystalReportViewer1.ReportSource = rpt

    End Sub

    Private Function FormaterSelection(szSelection As String) As String

        If szSelection.Length > 0 Then
            szSQL = Replace(szSelection, "gUserAgence", gUserAgence)
            szSQL = Replace(szSQL, "gUserCode", gUserCode)
            szSQL = Replace(szSQL, "gCD_CLI", gCD_CLI)
            szSQL = Replace(szSQL, "gDate_debut", Format(gDate_debut, "yyyy/MM/dd"))
            szSQL = Replace(szSQL, "gDate_fin", Format(gDate_fin, "yyyy/MM/dd"))
            szSQL = Replace(szSQL, "gMoniteur1", gMoniteur1)
            szSQL = Replace(szSQL, "gMoniteur2", gMoniteur2)
            szSQL = Replace(szSQL, "gNumero", gNumero)
            szSQL = Replace(szSQL, "gTypePlanning", gTypePlanning)
            szSQL = Replace(szSQL, "gSemaine", gSemaine)
            szSQL = Replace(szSQL, "gGroupe", gGroupe)
        Else
            szSQL = ""
        End If
        FormaterSelection = szSQL

    End Function
End Class